package com.xmut.rc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmut.rc.common.Result;
import com.xmut.rc.entity.RentInfo;
import com.xmut.rc.entity.vo.RentInfoVo;
import com.xmut.rc.service.CarInfoService;
import com.xmut.rc.service.RentInfoService;
import com.xmut.rc.service.RentInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 出租信息表 前端控制器
 * </p>
 *
 * @author ccm
 * @since 2022-06-26
 */
@RestController
@RequestMapping("/rc/rent-info")
public class RentInfoController {
    @Autowired
    private RentInfoService rentInfoService;

    @Autowired
    private CarInfoService carInfoService;

    /**
     * 分页列表查询
     *
     * @param rentInfoVo
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping("/rentList")
    public Result<?> getRentList(RentInfoVo rentInfoVo) {
        System.out.println(rentInfoVo);
        QueryWrapper<RentInfoVo> rentInfoVoQueryWrapper = new QueryWrapper<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        rentInfoVoQueryWrapper.like("take_place", Optional.ofNullable(rentInfoVo.getTakePlace()).orElse("") + "%");
        rentInfoVoQueryWrapper.like("back_place", Optional.ofNullable(rentInfoVo.getBackPlace()).orElse("") + "%");
        rentInfoVoQueryWrapper.ge("begin_time", format.format(rentInfoVo.getBeginTime()));
        rentInfoVoQueryWrapper.ge("end_time", format.format(rentInfoVo.getEndTime()));
        if (StringUtils.isNotEmpty(rentInfoVo.getType()) && !"全部".equals(rentInfoVo.getType())) {
            rentInfoVoQueryWrapper.eq("type", rentInfoVo.getType());
        }
        if (StringUtils.isNotEmpty(rentInfoVo.getGear()) && !"全部".equals(rentInfoVo.getGear())) {
            rentInfoVoQueryWrapper.eq("gear", rentInfoVo.getGear());
        }
        if (rentInfoVo.getSeat() != null) {
            rentInfoVoQueryWrapper.eq("seat", rentInfoVo.getSeat().toString());
        }
        rentInfoVoQueryWrapper.eq("rri.status","已发布");
        List<RentInfoVo> rentList = rentInfoService.getRentList(rentInfoVoQueryWrapper);
        return Result.OK(rentList);
    }

    /**
     * 分页列表查询
     *
     * @param rentInfo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            RentInfo rentInfo) {
        Page<RentInfo> pageParam = new Page<>(pageNo, pageSize);
        QueryWrapper<RentInfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(rentInfo.getCarId())){
            queryWrapper.eq("car_id",rentInfo.getCarId());
        }
        IPage<RentInfo> pageList = rentInfoService.page(pageParam, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping("/adminList")
    public Result<?> adminList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            RentInfo rentInfo) {
        Page<RentInfo> pageParam = new Page<>(pageNo, pageSize);
        QueryWrapper<RentInfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(rentInfo.getCarId())){
            queryWrapper.eq("car_id",rentInfo.getCarId());
        }
        IPage<RentInfo> pageList = rentInfoService.page(pageParam, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "通过审核", notes = "通过审核")
    @GetMapping(value = "/accept")
    public Result<?> accept(@RequestParam(name = "id", required = true) String rentId) {
        rentInfoService.editStatus(rentId,"已发布");
        return Result.OK("操作成功！");
    }

    @ApiOperation(value = "拒绝审核", notes = "拒绝审核")
    @GetMapping(value = "/reject")
    public Result<?> reject(@RequestParam(name = "id", required = true) String rentId) {
        rentInfoService.editStatus(rentId,"未发布");
        rentInfoService.removeById(rentId);
        return Result.OK("操作成功！");
    }

    @ApiOperation(value = "预约车辆", notes = "预约车辆")
    @GetMapping(value = "/order")
    public Result<?> order(@RequestParam(name = "id", required = true) String carId) {
        RentInfo rentInfo = rentInfoService.getOne(new QueryWrapper<RentInfo>().eq("car_id", carId));
        rentInfoService.editStatus(rentInfo.getId(),"已预约");
        return Result.OK("操作成功！");
    }

    @ApiOperation(value = "取消发布", notes = "取消发布")
    @GetMapping(value = "/cancel")
    public Result<?> cancel(@RequestParam(name = "id", required = true) String carId) {
        RentInfo rentInfo = rentInfoService.getOne(new QueryWrapper<RentInfo>().eq("car_id", carId));
        rentInfoService.editStatus(rentInfo.getId(),"已发布");
        return Result.OK("操作成功！");
    }

    /**
     * 添加
     *
     * @param rentInfo
     * @return
     */
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody RentInfo rentInfo) {
        rentInfoService.save(rentInfo);
        carInfoService.editStatus(rentInfo.getCarId(), "待审核");
        return Result.OK("添加成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        RentInfo rentInfo = rentInfoService.getById(id);
        if (rentInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(rentInfo);
    }

    /**
     * 编辑
     *
     * @param rentInfo
     * @return
     */
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody RentInfo rentInfo) {
        rentInfoService.updateById(rentInfo);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/deleteById")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        RentInfo rent = rentInfoService.getById(id);
        carInfoService.editStatus(rent.getCarId(), "未发布");
        rentInfoService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        rentInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
}

